package com.boot.common.entity;

import com.boot.system.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

public record CustomUserDetails(SysUser user) implements UserDetails {

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String[] roles = user.getRoles().split(",");
        List<GrantedAuthority> list = new ArrayList<>();
        for (String role : roles) {
            list.add(new SimpleGrantedAuthority(role));
        }
        return list;
    }

    @Override
    public SysUser user() {
        return user;
    }

    public Long getId(){
        return user.getId();
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    /**
     * 账号是否未过期
     * @return 结果
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号是否未锁定
     * @return 结果
     */
    @Override
    public boolean isAccountNonLocked() {
        return user.getStatus() == null || user.getStatus() == 1;
    }

    /**
     * 账号凭证是否未过期
     * @return 结果
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账号是否可用
     * @return 结果
     */
    @Override
    public boolean isEnabled() {
        return user.getStatus() == null || user.getStatus() == 1;
    }

}